package com.jieshuibao.controller;

import java.util.Date;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jieshuibao.model.db.Gainmedal;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.domain.CookieUser;
import com.jieshuibao.model.domain.MedalGetRequest;
import com.jieshuibao.model.domain.MedalResponse;
import com.jieshuibao.model.domain.MedalTypeResponse;
import com.jieshuibao.model.domain.NoticeResponse;
import com.jieshuibao.service.GainmedalService;
import com.jieshuibao.service.RedisService;
import com.jieshuibao.service.UserService;
import com.jieshuibao.util.MoneyRandom;

@Controller
@RequestMapping(value = "/medal")
public class MedalController {
	private static final Logger logger = LoggerFactory
			.getLogger("MedalController.class");

	private static final ObjectMapper mapper = new ObjectMapper();

	@Resource
	private RedisService redisService;

	@Resource
	private UserService userService;

	@Resource
	private GainmedalService gainmedalService;

	// 获得勋章 悬赏勋章 分享勋章
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	@ResponseBody
	public Object queryMedal(HttpServletRequest request,
			@RequestBody MedalGetRequest medalGetRequest) {
		MedalTypeResponse medalTypeResponse = new MedalTypeResponse();
		try {
			java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			//Date fifteenthDate1 = sdf.parse("2017-01-25 20:00:00");
			Date fifteenthDate2 = sdf.parse("2017-02-11 20:00:00");
			Date date = new Date();
			// if (date.before(fifteenthDate1)) {
			// medalTypeResponse.setCode("4");
			// medalTypeResponse.setDes("活动未开始");
			// } else
			if (date.after(fifteenthDate2)) {
				medalTypeResponse.setCode("4");
				medalTypeResponse.setDes("活动已结束");
			} else {
				String cookieId = request.getHeader("cookieId");
				logger.info("接收：" + mapper.writeValueAsString(medalGetRequest));
				if (cookieId != null) {
					CookieUser cookieUser = redisService.getObejct(cookieId,
							CookieUser.class);

					if (cookieUser != null) {
						String phoneNum = cookieUser.getPhoneNumber();
						if (phoneNum != null) {
							User user = userService
									.getUserByPhoneNumber(phoneNum);
							if (user != null) {
								String medalType = medalGetRequest
										.getMedalType();
								if (medalType != null && !medalType.equals("")) {
									Gainmedal gainmedal = gainmedalService
											.getGainmedalByPhone(phoneNum);
									Gainmedal gainmedal2 = new Gainmedal();
									if (medalType.equals("reward")) {
										if (gainmedal == null) {
											// 第一次得勋章

											gainmedal2.setPhone(phoneNum);
											gainmedal2.setMedalNum4(1);
											gainmedalService
													.addGainmedal(gainmedal2);
										} else {
											int medalNum4 = gainmedal
													.getMedalNum4();
											gainmedal2.setPhone(phoneNum);
											gainmedal2
													.setMedalNum4(medalNum4 + 1);
											gainmedalService
													.updateGainmedal(gainmedal2);
										}
										medalTypeResponse.setCode("1");
										medalTypeResponse.setDes("恭喜您点亮一个悬赏勋章");
										medalTypeResponse.setMedaltype("4");
										gainmedalService
												.getMedalNotice(4, user);

									} else if (medalType.equals("share")) {
										if (gainmedal == null) {
											// 第一次得勋章

											gainmedal2.setPhone(phoneNum);
											gainmedal2.setMedalNum5(1);
											gainmedalService
													.addGainmedal(gainmedal2);

										} else {
											int medalNum4 = gainmedal
													.getMedalNum5();
											gainmedal2.setPhone(phoneNum);
											gainmedal2
													.setMedalNum5(medalNum4 + 1);
											gainmedalService
													.updateGainmedal(gainmedal2);
										}
										medalTypeResponse.setCode("1");
										medalTypeResponse.setDes("恭喜您点亮一个分享勋章");
										medalTypeResponse.setMedaltype("5");
										gainmedalService
												.getMedalNotice(5, user);

									} else {
										// 数据格式错误
										medalTypeResponse.setCode("3");
										medalTypeResponse.setDes("数据格式错误");
									}
								} else {
									// 数据格式错误
									medalTypeResponse.setCode("3");
									medalTypeResponse.setDes("勋章类型错误");
								}
							} else {
								// 数据格式错误
								medalTypeResponse.setCode("2");
								medalTypeResponse.setDes("请重新登录");
							}

						} else {
							// 未登录 登陆时间过期
							medalTypeResponse.setCode("2");
							medalTypeResponse.setDes("请重新登录");
						}
					} else {
						// 未登录
						medalTypeResponse.setCode("2");
						medalTypeResponse.setDes("请重新登录");
					}

				} else {
					// 未登录
					medalTypeResponse.setCode("2");
					medalTypeResponse.setDes("请重新登录");
				}

			}

			logger.info("返回：" + mapper.writeValueAsString(medalTypeResponse));
		} catch (Exception e) {
			e.printStackTrace();
			medalTypeResponse.setCode("0");
			medalTypeResponse.setDes("系统繁忙请稍后再试！");
		}

		return medalTypeResponse;
	}

	// 勋章列表
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public Object queryMedalList(HttpServletRequest request) {
		MedalResponse medalResponse = new MedalResponse();
		try {

			String cookieId = request.getHeader("cookieId");
			logger.info("接收：" + mapper.writeValueAsString(cookieId));
			if (cookieId != null) {
				CookieUser cookieUser = redisService.getObejct(cookieId,
						CookieUser.class);

				if (cookieUser != null) {
					String phoneNum = cookieUser.getPhoneNumber();
					if (phoneNum != null) {
						User user = userService.getUserByPhoneNumber(phoneNum);
						if (user != null) {
							Gainmedal gainmedal = gainmedalService
									.getGainmedalByPhone(phoneNum);
							medalResponse.setCode("1");
							medalResponse.setGainmedal(gainmedal);
						} else {
							// 数据格式错误
							medalResponse.setCode("2");
							medalResponse.setDes("请重新登录");
						}

					} else {
						// 未登录
						medalResponse.setCode("2");
						medalResponse.setDes("请重新登录");
					}
				} else {
					// 未登录
					medalResponse.setCode("2");
					medalResponse.setDes("请重新登录");
				}

			} else {
				// 未登录
				medalResponse.setCode("2");
				medalResponse.setDes("请重新登录");
			}
			logger.info("返回：" + mapper.writeValueAsString(medalResponse));
		} catch (Exception e) {
			e.printStackTrace();
			medalResponse.setCode("0");
			medalResponse.setDes("系统繁忙请稍后再试！");
		}

		return medalResponse;
	}

	// 拆福袋 获得勋章

	@RequestMapping(value = "/open", method = RequestMethod.POST)
	@ResponseBody
	public Object openMedal(HttpServletRequest request) {
		MedalTypeResponse medalTypeResponse = new MedalTypeResponse();
		try {

			String cookieId = request.getHeader("cookieId");
			logger.info("接收：" + mapper.writeValueAsString(cookieId));
			if (cookieId != null) {
				CookieUser cookieUser = redisService.getObejct(cookieId,
						CookieUser.class);

				if (cookieUser != null) {
					String phoneNum = cookieUser.getPhoneNumber();
					if (phoneNum != null) {
						User user = userService.getUserByPhoneNumber(phoneNum);
						if (user != null) {
							
							// 计算时间
							java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
									"yyyy-MM-dd HH:mm:ss");
							
							Date endDate = sdf.parse("2017-02-05 20:00:00");
							Date date = new Date();
							if(date.after(endDate)){
								medalTypeResponse.setCode("3");
								medalTypeResponse.setDes("活动已结束");
								return medalTypeResponse;
							}
							Date eveDate1 = sdf.parse("2017-01-27 20:00:00");
							Date eveDate2 = sdf.parse("2017-01-28 00:00:00");
							Date fiveDate1 = sdf.parse("2017-02-01 18:00:00");
							Date fiveDate2 = sdf.parse("2017-02-01 22:00:00");
							Date fifteenthDate1 = sdf
									.parse("2017-02-11 16:00:00");
							Date fifteenthDate2 = sdf
									.parse("2017-02-11 20:00:00");
							
							Gainmedal gainmedal2 = new Gainmedal();
							Gainmedal gainmedal = gainmedalService
									.getGainmedalByPhone(phoneNum);
							gainmedal2.setPhone(phoneNum);
							if ((date.after(eveDate1) && date.before(eveDate2))
									|| (date.after(fiveDate1) && date
											.before(fiveDate2))
									|| (date.after(fifteenthDate1) && date
											.before(fifteenthDate2))) {
								// 偷偷看勋章 创作勋章 咨询勋章 50% 打赏勋章50% 还有红包
							/*	Random random2 = new Random();
								int nextInt2 = random2.nextInt(10);
								int[] a={2,2,3,3,3,3,3,3,3,3}; 
								int nextInt3=a[nextInt2];*/
								double newYearRed = 0.0;
								Random random = new Random();
								int nextInt = random.nextInt(10);
								if (nextInt % 2 == 0) {
									newYearRed = MoneyRandom.newYearRed();
								}
								medalTypeResponse.setMoney(newYearRed);
								if (nextInt % 2 == 0) {
									// 三个随机 发个红包 修改用户 修改勋章
									int i = gainmedalService
											.updateAndAddGainmedal(gainmedal,
													newYearRed, user);
									if (i == 1) {
										medalTypeResponse.setCode("1");
										medalTypeResponse
												.setDes("恭喜您点亮一个偷偷看勋章");
										medalTypeResponse.setMedaltype("1");
										medalTypeResponse.setMoney(newYearRed);
										return medalTypeResponse;
									} else if (i == 2) {
										medalTypeResponse.setCode("1");
										medalTypeResponse.setDes("恭喜您点亮一个咨询勋章");
										medalTypeResponse.setMedaltype("2");
										medalTypeResponse.setMoney(newYearRed);
										return medalTypeResponse;
									} else if (i == 3) {
										medalTypeResponse.setCode("1");
										medalTypeResponse.setDes("恭喜您点亮一个创作勋章");
										medalTypeResponse.setMedaltype("3");
										medalTypeResponse.setMoney(newYearRed);
										return medalTypeResponse;
									} else {
										medalTypeResponse.setCode("0");
										medalTypeResponse.setDes("网络抖动");
										return medalTypeResponse;
									}

								} else {
									// 打赏勋章
									int i = gainmedalService
											.updateAndAddGainmedalTwo(
													gainmedal, newYearRed, user);
									if (i == 6) {
										medalTypeResponse.setCode("1");
										medalTypeResponse.setDes("恭喜您点亮一个打赏勋章");
										medalTypeResponse.setMedaltype("6");
										medalTypeResponse.setMoney(newYearRed);
										return medalTypeResponse;
									} else {
										medalTypeResponse.setCode("0");
										medalTypeResponse.setDes("网络抖动");
										return medalTypeResponse;
									}
								}

							} else {
								// 随机获得三个勋章
								// 1偷偷看勋章 2咨询勋章 3创作勋章 4悬赏勋章 5 分享勋章 6 打赏勋章

								if (gainmedal != null) {
									Integer medalNum1 = gainmedal
											.getMedalNum1();
									Integer medalNum2 = gainmedal
											.getMedalNum2();
									Integer medalNum3 = gainmedal
											.getMedalNum3();
									String randomRange = gainmedal
											.getRandomRange();
									String[] split = randomRange.split(",");
									if (split != null) {
										StringBuilder stringBuilder = new StringBuilder();
										stringBuilder.append(randomRange);
										int a = split.length;
										Random r = new Random();
										int indexInt = r.nextInt(a);
										String str = split[indexInt];
										if (str.equals("1")) {
											// 获得一个偷偷看勋章
											if (medalNum1 != null) {
												gainmedal2
														.setMedalNum1(medalNum1 + 1);
												if (medalNum1 >= 0) {
													int indexOf = stringBuilder
															.indexOf("1");
													if (indexOf == 0) {
														stringBuilder.delete(
																indexOf,
																indexOf + 2);
													} else if (indexOf > 0) {
														stringBuilder.delete(
																indexOf - 1,
																indexOf + 1);
													}
													gainmedal2
															.setRandomRange(stringBuilder
																	.toString());
												}
											} else {
												gainmedal2.setMedalNum1(1);
												gainmedal2
														.setRandomRange("2,3");
											}
											gainmedalService
													.updateGainmedal(gainmedal2);
											medalTypeResponse.setCode("1");
											medalTypeResponse
													.setDes("恭喜您点亮一个偷偷看勋章");
											medalTypeResponse.setMedaltype("1");
											gainmedalService.getMedalNotice(1,
													user);
											return medalTypeResponse;
										} else if (str.equals("2")) {
											if (medalNum2 != null) {
												gainmedal2
														.setMedalNum2(medalNum2 + 1);
												if (medalNum2 >= 0) {
													int indexOf = stringBuilder
															.indexOf("2");
													if (indexOf == 0) {
														stringBuilder.delete(
																indexOf,
																indexOf + 2);
													} else if (indexOf > 0) {
														stringBuilder.delete(
																indexOf - 1,
																indexOf + 1);
													}
													gainmedal2
															.setRandomRange(stringBuilder
																	.toString());
												}
											} else {
												gainmedal2.setMedalNum2(1);
												gainmedal2
														.setRandomRange("1,3");
											}
											gainmedalService
													.updateGainmedal(gainmedal2);

											medalTypeResponse.setCode("1");
											medalTypeResponse
													.setDes("恭喜您点亮一个咨询勋章");
											medalTypeResponse.setMedaltype("2");
											gainmedalService.getMedalNotice(2,
													user);
											return medalTypeResponse;
										} else if (str.equals("3")) {
											if (medalNum3 != null) {
												gainmedal2
														.setMedalNum3(medalNum3 + 1);
												if (medalNum3 >= 0) {
													int indexOf = stringBuilder
															.indexOf("3");
													if (indexOf == 0) {
														stringBuilder.delete(
																indexOf,
																indexOf + 2);
													} else if (indexOf > 0) {
														stringBuilder.delete(
																indexOf - 1,
																indexOf + 1);
													}
													gainmedal2
															.setRandomRange(stringBuilder
																	.toString());
												}

											} else {
												gainmedal2.setMedalNum3(1);
												gainmedal2
														.setRandomRange("1,2");
											}
											gainmedalService
													.updateGainmedal(gainmedal2);

											medalTypeResponse.setCode("1");
											medalTypeResponse
													.setDes("恭喜您点亮一个创作勋章");
											medalTypeResponse.setMedaltype("3");
											gainmedalService.getMedalNotice(3,
													user);
											return medalTypeResponse;

										} else {
											// 随机获得一个
											int b = (int) (1 + Math.random() * (3));
											if (b == 1) {
												// 1偷偷看勋章
												gainmedal2
														.setMedalNum1(medalNum1 + 1);
												medalTypeResponse
														.setDes("恭喜您点亮一个偷偷看勋章");
												medalTypeResponse
														.setMedaltype("1");

											} else if (b == 2) {
												gainmedal2
														.setMedalNum2(medalNum2 + 1);
												medalTypeResponse
														.setDes("恭喜您点亮一个咨询勋章");
												medalTypeResponse
														.setMedaltype("2");
											} else {
												gainmedal2
														.setMedalNum3(medalNum3 + 1);

												medalTypeResponse
														.setDes("恭喜您点亮一个创作勋章");
												medalTypeResponse
														.setMedaltype("3");
											}
											gainmedalService.getMedalNotice(b,
													user);
											gainmedalService
													.updateGainmedal(gainmedal2);
											medalTypeResponse.setCode("1");
											return medalTypeResponse;
										}
									}

								} else {
									// 第一次
									int a = (int) (1 + Math.random() * (3));
									if (a == 1) {
										// 1偷偷看勋章
										gainmedal2.setMedalNum1(1);
										gainmedal2.setRandomRange("2,3");
										medalTypeResponse
												.setDes("恭喜您点亮一个偷偷看勋章");
										medalTypeResponse.setMedaltype("1");
									} else if (a == 2) {
										gainmedal2.setMedalNum2(1);
										gainmedal2.setRandomRange("1,3");
										medalTypeResponse.setDes("恭喜您点亮一个咨询勋章");
										medalTypeResponse.setMedaltype("2");
									} else {
										gainmedal2.setMedalNum3(1);
										gainmedal2.setRandomRange("1,2");
										medalTypeResponse.setDes("恭喜您点亮一个创作勋章");
										medalTypeResponse.setMedaltype("3");
									}
									medalTypeResponse.setCode("1");
									gainmedalService.getMedalNotice(a, user);
									gainmedalService.addGainmedal(gainmedal2);
									return medalTypeResponse;

								}

							}

						} else {
							// 数据格式错误
							medalTypeResponse.setCode("2");
							medalTypeResponse.setDes("请重新登录");
						}

					} else {
						// 未登录
						medalTypeResponse.setCode("2");
						medalTypeResponse.setDes("请重新登录");
					}
				} else {
					// 未登录
					medalTypeResponse.setCode("2");
					medalTypeResponse.setDes("请重新登录");
				}

			} else {
				// 未登录
				medalTypeResponse.setCode("2");
				medalTypeResponse.setDes("请重新登录");
			}
			logger.info("返回：" + mapper.writeValueAsString(medalTypeResponse));
		} catch (Exception e) {
			e.printStackTrace();
			medalTypeResponse.setCode("0");
			medalTypeResponse.setDes("系统繁忙请稍后再试！");
		}

		return medalTypeResponse;
	}

	// 通知谁的勋章

	@RequestMapping(value = "/notice", method = RequestMethod.POST)
	@ResponseBody
	public Object queryMedal(HttpServletRequest request) {
		NoticeResponse noticeResponse = new NoticeResponse();
		try {

			String cookieId = request.getHeader("cookieId");
			logger.info("接收：" + mapper.writeValueAsString(cookieId));
			if (cookieId != null) {
				CookieUser cookieUser = redisService.getObejct(cookieId,
						CookieUser.class);

				if (cookieUser != null) {
					String phoneNum = cookieUser.getPhoneNumber();
					if (phoneNum != null) {
						User user = userService.getUserByPhoneNumber(phoneNum);
						if (user != null) {
							String string = redisService.get("medal_code");

							noticeResponse.setCode("1");
							noticeResponse.setNotices(string);

						} else {
							// 数据格式错误
							noticeResponse.setCode("2");
							noticeResponse.setDes("请重新登录");
						}

					} else {
						// 未登录
						noticeResponse.setCode("2");
						noticeResponse.setDes("请重新登录");
					}
				} else {
					// 未登录
					noticeResponse.setCode("2");
					noticeResponse.setDes("请重新登录");
				}

			} else {
				// 未登录
				noticeResponse.setCode("2");
				noticeResponse.setDes("请重新登录");
			}
			logger.info("返回：" + mapper.writeValueAsString(noticeResponse));
		} catch (Exception e) {
			e.printStackTrace();
			noticeResponse.setCode("0");
			noticeResponse.setDes("系统繁忙请稍后再试！");
		}

		return noticeResponse;
	}
}
